﻿using System;
using System.Data;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Xml;
using System.Resources;
using EnvDTE;
using System.ComponentModel;

namespace SPVisualDev.VSAddin
{
    internal class ResourceHelper
    {
        internal static string ParseResourceKeyFromRawString(string RawString)
        {
            if (!IsResourceString(RawString))
                return null;

            string ResourceKey;

            int i = RawString.IndexOf(";");
            if (i == -1)
                i = RawString.Length;

            i = RawString.IndexOf(",");
            if (i == -1)
            {
                ResourceKey = RawString.Substring(11);
            }
            else
            {
                ResourceKey = RawString.Substring(i + 1);
            }

            if (ResourceKey.EndsWith(";"))
                ResourceKey = ResourceKey.Substring(0, ResourceKey.Length - 1);

            return ResourceKey;
        }

        internal static bool IsResourceString(string RawString)
        {
            if (string.IsNullOrEmpty(RawString))
                return false;
            else
                return RawString.StartsWith("$Resources:");
        }

        internal static ResourceKeyInfo ResolveRawStringDefinition(string RawString, string DefaultResourceFile, ProjectItem ParentFeatureFolder)
        {
            return ResolveRawStringDefinition(RawString, DefaultResourceFile, ParentFeatureFolder, null);
        }
        internal static ResourceKeyInfo ResolveRawStringDefinition(string RawString, string DefaultResourceFile, ProjectItem ParentFeatureFolder, string Culture)
        {
            ResourceKeyInfo res_key_info = new ResourceKeyInfo();
            string ResourceKey;

            if (RawString == null)
                throw new Exception("Resource string can not be null.");

            if (!RawString.StartsWith("$Resources:"))
                return res_key_info;

            int i = RawString.IndexOf(";");
            if (i == -1)
                i = RawString.Length;

            string res_file = RawString.Substring(0, i);

            i = res_file.IndexOf(",");
            if (i == -1)
            {
                res_file = DefaultResourceFile;
                ResourceKey = RawString.Substring(11);
            }
            else
            {
                res_file = RawString.Substring(11, i - 11);
                ResourceKey = RawString.Substring(i + 1);
            }

            if (ResourceKey.EndsWith(";"))
                ResourceKey = ResourceKey.Substring(0, ResourceKey.Length - 1);


            res_key_info.ResourceKey = ResourceKey;

            bool isLocalResource = (string.Compare(res_file, Common.C_LOCAL_RESOURCE_IDENTIFIER, true) == 0);


            if (isLocalResource)
                res_key_info.ResourceFile = "Resources";
            else
                res_key_info.ResourceFile = res_file;

            ProjectItem resources = null;
            try
            {
                if (isLocalResource)
                {
                    try
                    {
                        resources = ParentFeatureFolder.ProjectItems.Item("Resources");
                    }
                    catch { }

                }
                else
                {
                    try
                    {
                        resources = ParentFeatureFolder.ContainingProject.ProjectItems.Item("12").ProjectItems.Item("Resources");
                    }
                    catch { }
                }
            }
            catch { }



            if (resources != null)
            {
                try
                {
                    if (isLocalResource)
                    {
                        if (!string.IsNullOrEmpty(Culture))
                            res_key_info.ProjectItem = resources.ProjectItems.Item("Resources." + Culture + ".resx");
                        else
                            res_key_info.ProjectItem = resources.ProjectItems.Item("Resources.resx");
                        
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(Culture))
                            res_file += "." + Culture;

                        res_key_info.ProjectItem = resources.ProjectItems.Item(res_file + ".resx");
                    }
                }
                catch { }


                if (res_key_info.ProjectItem == null)
                {
                    //Changed to support client installation
                    /*
                    string file_path = Common.Get12HivePath() + "\\Resources\\" + res_file + ".resx";
                    if (File.Exists(file_path))
                    {
                        res_key_info.Path = file_path;
                    }
                     */
                }

                if (res_key_info.ProjectItem != null)
                    res_key_info.Path = Common.GetProjectItemPath(res_key_info.ProjectItem);

            }


            res_key_info.IsFeatureLocal = isLocalResource;
            res_key_info.ProjectItemCollection = resources.ProjectItems;

            return res_key_info;
        }

        internal static List<CultureFileInfo> GetAllAvailableCultures(string RawString, string DefaultResourceFile, ProjectItem ParentFeatureFolder)
        {
            CultureFileInfo cFileInfo;
            List<CultureFileInfo> list = new List<CultureFileInfo>();

            ResourceKeyInfo res_key_info = ResolveRawStringDefinition(RawString, DefaultResourceFile, ParentFeatureFolder);

            if (res_key_info.Path == null)
            {
                //Search for resource files other then default.
                if (res_key_info.ProjectItemCollection != null)
                {
                    foreach (ProjectItem item in res_key_info.ProjectItemCollection)
                    {
                        if (item.Name.ToLower().StartsWith(res_key_info.ResourceFile.ToLower() + ".") && Path.GetExtension(item.Name.ToLower()) == ".resx")
                        {
                            cFileInfo = new CultureFileInfo();
                            cFileInfo.ProjectItem = item;
                            cFileInfo.Path = Common.GetProjectItemPath(item);
                            cFileInfo.Culture = new CultureInfo(cFileInfo.Path.Split('.')[1]);

                            list.Add(cFileInfo);
                        }
                    }
                }
            }
            else
            {
                //Add default.
                cFileInfo = new CultureFileInfo();
                cFileInfo.ProjectItem = res_key_info.ProjectItem;
                cFileInfo.Path = res_key_info.Path;
                cFileInfo.Culture = CultureInfo.InvariantCulture;

                list.Add(cFileInfo);



                string f = Path.GetFileNameWithoutExtension(res_key_info.Path);
                string dir = Path.GetDirectoryName(res_key_info.Path);

                foreach (string file in Directory.GetFiles(dir))
                {
                    string file_name = Path.GetFileName(file);

                    if (!file_name.ToLower().EndsWith("resx"))
                        continue;


                    string s = Path.GetFileNameWithoutExtension(file_name);
                    if (s.ToLower().StartsWith(f.ToLower() + "."))
                    {
                        cFileInfo = new CultureFileInfo();
                        cFileInfo.Path = file;
                        cFileInfo.Culture = new CultureInfo(file_name.Split('.')[1]);

                        if (res_key_info.ProjectItem != null)
                        {
                            try
                            {
                                cFileInfo.ProjectItem = (res_key_info.ProjectItem.Collection as ProjectItems).Item(file_name);
                            }
                            catch { }
                        }


                        list.Add(cFileInfo);
                    }
                }
            }

            return list;
        }

        internal struct CultureFileInfo
        {
            public CultureInfo Culture;
            public ProjectItem ProjectItem;
            public string Path;
        }
    }

    internal struct ResourceKeyInfo
    {
        public string Path;
        public ProjectItems ProjectItemCollection;
        public ProjectItem ProjectItem;
        public bool IsFeatureLocal;
        public string ResourceKey;
        public string ResourceFile;
    }

}
